<template>
  <el-row type="flex" justify="space-between" style="width: 100%">
    <el-col :span="20"> {{ nodeData.name }}</el-col>
    <el-col :span="4">
      <el-row type="flex" justify="end">
        <!-- 两个内容 -->
        <el-col>{{ nodeData.manager }}</el-col>
        <!-- 下拉选择框 -->
        <el-col>
          <!-- 下拉菜单 element -->
          <el-dropdown @command="handleCommand">
            <span @click.stop> 操作<i class="el-icon-arrow-down" /> </span>
            <!-- 下拉菜单 -->
            <el-dropdown-menu slot="dropdown">
              <el-dropdown-item command="add">添加子部门</el-dropdown-item>
              <el-dropdown-item v-if="!isRoot" command="edit">查看部门</el-dropdown-item>
              <el-dropdown-item v-if="!isRoot" command="del">删除部门</el-dropdown-item>
            </el-dropdown-menu>
          </el-dropdown>
        </el-col>
      </el-row>
    </el-col>
  </el-row>
</template>

<script>
import { reqDelDepartments } from '@/api/departments'
export default {
  name: 'TreeTools',
  props: {
    nodeData: {
      type: Object,
      required: true
    },
    isRoot: {
      type: Boolean,
      default: false
    }
  },
  methods: {
    handleCommand(type) {
      // console.log(type)
      // 添加
      if (type === 'add') {
        // console.log('添加')
        // 1. 通知父组件展示对话框
        this.$emit('showAddDialog', this.nodeData)
      }
      if (type === 'edit') {
        // 查看/编辑
        // 通知父组件展示对话框
        this.$emit('showEditDialog', this.nodeData)
      }
      // 删除
      if (type === 'del') {
        // 1. 确认提示   简化$confirm写法  --- confirm(点了确认是confirm)
        this.$confirm('是否删除此部门?', '提示', {
          confirmButtonText: '确定', // 确认按钮的文本
          cancelButtonText: '取消', // 取消按钮的文本
          type: 'warning' // 小图标
        }).then(async() => {
          // 2. 发送请求
          const { message } = await reqDelDepartments(this.nodeData.id)
          // 3. 提示用户
          this.$message.success(message)
          // 4. 获取部门列表 通知父组件重新获取部门列表
          this.$emit('add-depts')
        }).catch(() => {
          this.$message.info('已取消删除')
        })
      }
    }
  }

}
</script>

<style>

</style>
